$(function(){
	
	$('#editForm').amValidator();
	
	$('#editForm').submit(function(){
		var validator = $(this).data('amui.validator').isFormValid(); 
        if(!validator){return false;}
        var data = $(this).amFormToJson();
        //妹子UI中的下拉多选， 无法获取到多个值，将重新手动取值
        var roles = new Array();
        $('#edit_roles').next(".am-selected").find("ul.am-selected-list li.am-checked").each(function(){
        	roles.push($(this).data("value"));
        }) ;
        
        data.roles = roles.join(",") ;
        $.post(window.rootPath + '/user/save', data, function(r) {
        	if (r.r) { 
        		location.href = window.rootPath + "/user";
            } else {
            	alert(r.m);
            }
        }, 'json');
        return false;
    });
	
	var user_roles = ($("#edit_roles").data("roles") || "") + "";
	var roles_arr = user_roles.split(",");
	for ( var i in user_roles) {
		$("#edit_roles").find("option[value='"+roles_arr[i]+"']").attr("selected", true);
	}
	$("#edit_roles").selectIt({btnWidth:'300px'});
	
	$('#cancelBtn').click(function(){
		
		location.href = window.rootPath + "/user";
		
	});
});